package com.zmytest.model;

import com.zmytest.util.Encryptor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * Created on 2018/12/21.
 *
 * @author zhangmingyu
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Entity
public class LoginRecord extends BaseModel<Long>{

    private boolean enable;

    private LocalDateTime expireTime;

    private String loginToken;

    private Long accountId;

    @ManyToOne
    @JoinColumn(name = "accountId",insertable = false,updatable = false)
    private Account account;

    public LoginRecord(){}

    public LoginRecord(Account account,long expireSeconds){
        this.enable = true;
        this.expireTime = LocalDateTime.now().plusSeconds(expireSeconds);
        String now = DateTimeFormatter.ofPattern("yyyyMMddHHmmss").format(LocalDateTime.now());
        String baseLoginToken = account.getName()+"zmytest"+now;
        this.loginToken = Encryptor.getMd5Code(baseLoginToken);
        this.accountId = account.getId();
    }

}
